import { defineStore } from 'pinia'
import { ref } from 'vue'
import { flattenTree } from '@/utils'

export const usePermissionStore = defineStore(
  'permission',
  () => {
    const menuList = ref([])
    const actionList = ref([])

    const setMenuList = (list) => (menuList.value = flattenTree(list))
    const setActionList = (list) => (actionList.value = list)

    const judgeMenuPermission = (path) => menuList.value.find((item) => item.path === path)
    const judgeActionPermission = (action) => actionList.value.find((item) => item === action)

    const clearUserPermission = () => {
      menuList.value = []
      actionList.value = []
    }

    return {
      menuList,
      actionList,
      setMenuList,
      setActionList,
      judgeMenuPermission,
      judgeActionPermission,
      clearUserPermission,
    }
  },
  {
    persist: true,
  },
)
